package MR_process.MR3;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;

import MR_process.Bean2;
import MR_process.Bean3;

public class Reduce3 extends Reducer<Text,Bean2,Bean3,NullWritable> {

    // private SimpleGroupFactory groupFactory;
    // private MessageType schema;
    @Override
    public void reduce(Text key, Iterable<Bean2> values, Context context) throws IOException, InterruptedException {
        long len = 0;
        // 将 values 的内容存入集合
        List<Bean2> beanList = new ArrayList<>();
        for (Bean2 bean : values) {
            beanList.add(new Bean2(bean.getWord(), bean.getId(), bean.getCount(),bean.getDistinct_id()));
            len+=bean.getCount();
        }

        // 遍历集合以输出结果
        for (Bean2 bean : beanList) {
            context.write(new Bean3(bean.getWord(),bean.getId(),bean.getCount(),len,bean.getDistinct_id()), NullWritable.get());
        }
    }

}
